package cn.com.jsepc.rep.comm;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import cn.com.jsepc.appframe.system.AppframeOrgClient;
import cn.com.jsepc.appframe.system.AppframePermissionClient;
import cn.com.jsepc.appframe.system.domain.SpecialOrg;
import cn.com.jsepc.appframe.system.domain.SysSpRole;
import cn.com.jsepc.appframe.system.domain.SysSystem;
import cn.com.jsepc.appframe.system.service.AppframeOrg;
import cn.com.jsepc.appframe.system.service.AppframePermission;
import cn.com.jsepc.rep.vo.TreeVO;

public class LocalPurview {
	AppframeOrg appframeorg=null;
	public LocalPurview()
	{
		appframeorg=AppframeOrgClient.getInstance();
	}
	public String getOrg_Role(String orginfoid,String specialOrgInfo){
		JSONArray json = new JSONArray();
		String [] lstorg=null;
		String orgTypeid="";
		//系统代码
		orgTypeid="BI_REP";//MIDAWA
		//数据权限
		//datatype =ReadProperties.getValue("");
		
		SysSystem system = appframeorg.getSystemByCode(orgTypeid);
		if(orginfoid==null||orginfoid.equalsIgnoreCase("")||orginfoid.equalsIgnoreCase("null"))
		{
			//specialOrgInfo=StringUtils.toGBK(specialOrgInfo);
//			try {
//				System.out.println("specialOrgInfo>>>>>>>>>>>>>>>>>>>>"+specialOrgInfo);
//				//specialOrgInfo=new String(specialOrgInfo.getBytes("ISO-8859-1"),"GBK");
//			} catch (UnsupportedEncodingException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			List lstResult=new ArrayList();
			String s[]=specialOrgInfo.split(ConstUtil.splitChar);
			for(int i=0;i<s.length;i++){
				if(s[i].equals(""))continue;
				String text[]=s[i].split(",");
				TreeVO orgroletree=new TreeVO();
				//orgroletree.setId(speorg[0].getId()+",ORG"+","+speorg[0].getCode()+","+speorg[0].getUnicode()+","+speorg[0].getIsLeaf()+","+speorg[0].getLevel());
				//orgroletree.setText(speorg[0].getName());
				orgroletree.setId(s[i]);
				orgroletree.setText(text[text.length-1]);
				lstResult.add(orgroletree);
			}
			//SpecialOrg[] speorg= appframeorg.getRootSpecialOrgs(system.getOrg_type_id(),null);
			if(lstResult.size()>0){
				json =JSONArray.fromObject(lstResult);
				return json.toString();
			}else{
				return "";
			}
			
		}
		else
		{
			List lstResult=new ArrayList();
			try{
				lstorg=orginfoid.split(",");
			}catch(Exception exp)
			{
				System.out.println("cn.com.jsepc.midawa.role.OrgRole.getOrg_Role()");
				System.out.println(exp.getMessage());
				exp.printStackTrace();
			}
			
			String parentid=lstorg[0];
			String org=lstorg[1];
			TreeVO orgroletree=new TreeVO();
			if(!org.equalsIgnoreCase("ROLE"))
			{
				orgroletree.setId(parentid+",ROLE");
				orgroletree.setText("角色");
				orgroletree.setLeaf(false);
				lstResult.add(orgroletree);
			}
			if(org.equalsIgnoreCase("ORG"))
			{
				//SpecialOrg[] specorg= appframeorg.getChildrenSpecialOrgs (new Long(Long.parseLong(parentid)));
				SpecialOrg[] specorg= appframeorg.getChildrenSpecialOrgsOfCorp(new Long(Long.parseLong(parentid)));
				//SpecialOrg[] specorg= appframeorg.getLowerSpecialOrgs (new Long(Long.parseLong(parentid)));
				if(specorg!=null)
				{
					for(int i=0;i<specorg.length;i++)
					{
						String state=specorg[i].getState();
						if(state.trim().equals("Y")){
							orgroletree=new TreeVO();
							orgroletree.setId(specorg[i].getId()+",ORG");
							orgroletree.setText(specorg[i].getName());
							orgroletree.setLeaf(false);
							lstResult.add(orgroletree);
						}
					}
				}
				json =JSONArray.fromObject(lstResult);
				return json.toString();
			}else
			{
				SysSpRole[] sysrole=appframeorg.getSpRolesBySpecialOrg(new Long(Long.parseLong(parentid)) ,system.getId());
				AppframePermission permission= AppframePermissionClient.getInstance();
				//Map resMap = permission.getSprolesAndDatasets(orgTypeid,new Long(Long.parseLong(parentid)),null,null,datatype,0);
				if(sysrole!=null)
				{
					for(int j=0;j<sysrole.length;j++)
					{
						orgroletree=new TreeVO();
						//Long [] dataset = (Long [])resMap.get(sysrole[j].getId());
//						for(int k=0;k<dataset.length;k++)
//						{
//							System.out.println("dataset["+j+"]:"+dataset[k]);
//						}
						orgroletree.setId(sysrole[j].getCode()+","+sysrole[j].getId()+","+sysrole[j].getRole_id());
//						System.out.println(orgroletree.getId());
						orgroletree.setText(sysrole[j].getName());
						orgroletree.setLeaf(true);
						lstResult.add(orgroletree);
					}
				}
				json =JSONArray.fromObject(lstResult);
				return json.toString();
			}
		}
		
	
	}
}
